package com.juyu.miliao.api.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.juyu.miliao.api.domain.UserVideo;
import com.juyu.miliao.api.service.UserVideoService;
import com.juyu.miliao.api.util.JacksonView;
import com.juyu.miliao.api.validator.common.Save;
import com.juyu.miliao.common.domain.BaseQuery;
import com.juyu.miliao.common.domain.Subject;

import net.sf.json.JSONObject;

import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户视频功能
 *
 * @author jh
 */
@RestController
@RequestMapping("/video")
public class UserVideoController {

	@Resource
	private UserVideoService userVideoService;

	/**
	 * 获取用户视频列表,审核通过的
	 *
	 * @param viewUserId
	 *            被查看的用户id
	 * @return List<UserVideo>
	 */
	@GetMapping(value = "/{userId}/{pageNum}-{pageSize}")
	@JsonView(JacksonView.UserVideo.class)
	public List<UserVideo> list( @PathVariable("userId") Long viewUserId,
			@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
		return userVideoService.list(viewUserId, pageNum, pageSize);
	}

	/**
	 * 获取用户视频列表
	 *
	 * @param token
	 *            token
	 * @param viewUserId
	 *            查看的用户id
	 * @return list
	 */
	@GetMapping(value = "/detail/{userId}/{pageNum}-{pageSize}")
	@JsonView(JacksonView.UserVideo.class)
	public List<UserVideo> listDetail(Authentication token,
			@PathVariable("userId") Long viewUserId, @PathVariable("pageNum") Integer pageNum,
			@PathVariable("pageSize") Integer pageSize) {
		Subject subject = (Subject) token.getPrincipal();
		return userVideoService.listDetail(subject.getId(), viewUserId, pageNum,pageSize);
	}

	/**
	 * 保存视频
	 *
	 * @param token
	 *            token
	 * @param userVideo
	 *            userVideo
	 */
	@PostMapping(value = "")
	public void save(Authentication token, @Validated(value = Save.class) UserVideo userVideo) {
		Subject subject = (Subject) token.getPrincipal();
		Long userId = subject.getId();
		userVideoService.save(userId, userVideo);
	}

	/**
	 * 删除
	 *
	 * @param token
	 *            token
	 * @param videoId
	 *            videoId
	 */
	@DeleteMapping(value = "/{id}")
	public void delete(Authentication token, @PathVariable(value = "id") Long videoId) {
		Subject subject = (Subject) token.getPrincipal();
		Long userId = subject.getId();
		userVideoService.delete(userId, videoId);
	}

	// /**
	// * 获取视频列表
	// *
	// * @param type 1.热门 2.最新 3.特权
	// * @param page
	// * @param pageSize
	// * @return
	// */
	// @GetMapping("/list/{type}/{page}-{pageSize}")
	// public List<UserVideo> getUserVideoList(@PathVariable("type") Integer
	// type, @PathVariable("page") Integer page,
	// @PathVariable("pageSize") Integer pageSize) {
	// return userVideoService.getUserVideoList(type, page, pageSize);
	// }
	//

	/**
	 * 视频点赞
	 *
	 * @param videoId
	 *            视频id
	 */
	@GetMapping("/love/{videoId}")
	public JSONObject addUserVideoLove(Authentication token, @PathVariable("videoId") long videoId) {
		Subject subject = (Subject) token.getPrincipal();
		Long userId = subject.getId();
		return userVideoService.clickVideoLove(userId, videoId);
	}

}
